<?php

class AuthController extends Zend_Controller_Action
{

    public function init()
    {
$this->initView();
$this->view->baseUrl = $this->_request->getBaseUrl();

    }

    public function indexAction()
    {
		$this->_redirect('/');


    }
    
    public function loginAction()
    {
    	
//    	$this->view->layout()->setLayout('admin');
    	$this->view->Breadcrumbs = array(array('Link'=>'/','Name'=>'Home'),   array('Link'=>'auth/login','Name'=>'Login'));
    	if ($this->_request->isPost())
    	{
    		Zend_Loader::loadClass("Zend_Filter_StripTags");
    		Zend_Loader::loadClass("Zend_Auth_Adapter_DbTable");
    		$f = new Zend_Filter_StripTags();
    		$username = $f->filter($this->_request->getPost('username'));
    		$password = $f->filter($this->_request->getPost('password'));
    		
    		$dbAdapter = Zend_Registry::get('dbAdapter');
    		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    		$authAdapter->setTableName('users');
    		$authAdapter->setIdentityColumn('username');
    		$authAdapter->setCredentialColumn('password');
    		
    		$authAdapter->setIdentity($username);
    		$authAdapter->setCredential($password);
    		
    		$auth = Zend_Auth::getInstance();
    		$result = $auth->authenticate($authAdapter);
    		
    		if($result->isValid())
    		{
    			$data = $authAdapter->getResultRowObject(null,'password');
    			$auth->getStorage()->write($data);
    			$this->_redirect('/admin');
    		}
    		else
    		{
    			
    		}
    		$this->render();
    	}
    }
    public function logoutAction()
    {
    	Zend_Auth::getInstance()->clearIdentity();
    	$this->_redirect('/');
    }


}

